
function switch_page(name, p, cb) {
    $.get(context.config.page_url + name + ".html", function(r) {
        if (!p) {
            $("#game").empty();
            $("#game").html(r);
        }
        else
            p.html(r);
        
        if (cb) cb();
    });
}

function alert2(message, warning) {
    warning = warning === undefined ? true : warning;
    layer.alert(message, {
        icon: warning ? 2 : 1,
    });
}

// 今日注单的逻辑
function today() {
    Util.queryTemplate($(".today .zy-table tbody"), ".today .zxf_pagediv", $(".today .f10 button"), 
    function(page, size, cb) {
        var data = {
            pageSize: size,
            pageNum: page,
            start: new Date().Format("yyyy-MM-dd"),
            end: new Date().Format("yyyy-MM-dd"),
        };
        var v = $(".today .f1 input").val().trim();
        if (v) {
            data.uid = v;
        }
        v = $(".today .f2 select").val();
        if (v != -1) {
            data.type = v;
        }
        v = $(".today .f3 select").val();
        if (v != -1) {
            data.state = v;
        }
        CMD.bet_rquery(data, cb);
    }, function(table, list, sum) {
        if (Util.isEmpty(list)) {
            table.append('<tr><td colspan="10">暂无数据</td></tr>');
            return;
        }

        // 合计
        var tr = $("<tr></tr>");
        tr.append("<td colspan='5'>合计</td>");
        tr.append("<td>" + sum.amount/100 + "</td>");
        tr.append("<td>" + sum.profit/100 + "</td>");
        tr.append("<td>" + sum.back/100 + "</td>");
        tr.append("<td>" + sum.rprofit/100 + "</td>");
        tr.append("<td>" + sum.rback/100 + "</td>");
        table.append(tr);

        // 明细
        for (var i in list) {
            var o = list[i];
            var tr = $("<tr></tr>");
            //tr.append("<td>" + o.id + "</td>");
            tr.append("<td>" + Util.converServerTime(o.time) + "</td>");
            tr.append("<td>" + o.user.mobile + "</td>");
            tr.append("<td>" + get_game_display_name(o.type) + "</td>");
            tr.append("<td>" + o.issue + "</td>");
            tr.append("<td>" + Util.convertTarget(o.type, o.target) + "@" + o.rate + "</td>");
            tr.append("<td>" + o.amount/100 + "</td>");
            tr.append("<td>" + (o.profit/100).toFixed(3) + "</td>");
            tr.append("<td>" + (o.back || 0)/100 + "</td>");
            tr.append("<td>" + (o.rprofit || 0)/100 + "</td>");
            tr.append("<td>" + (o.rback || 0)/100 + "</td>");
            table.append(tr);
        }
    });
}

// 历史注单
function history() {
    Util.queryTemplate($(".history .zy-table tbody"), ".history .zxf_pagediv", $(".history .f10 button"), 
    function(page, size, cb) {
        var data = {
            pageSize: size,
            pageNum: page,
        };
        var v = $(".history .f1 input").val().trim();
        if (v) {
            data.uid = v;
        }
        v = $(".history .f2 select").val();
        if (v != -1) {
            data.type = v;
        }
        v = $(".history .f3 select").val();
        if (v != -1) {
            data.state = v;
        }
        v = $(".history .f4 input").val();
        if (v) data.start = v;
        v = $(".history .f5 input").val();
        if (v) data.end = v;
        CMD.bet_rquery(data, cb);
    }, function(table, list, sum) {
        if (Util.isEmpty(list)) {
            table.append('<tr><td colspan="8">暂无数据</td></tr>');
            return;
        }

        // 合计
        var tr = $("<tr></tr>");
        tr.append("<td colspan='5'>合计</td>");
        tr.append("<td>" + sum.amount/100 + "</td>");
        tr.append("<td>" + sum.profit/100 + "</td>");
        tr.append("<td>" + sum.back/100 + "</td>");
        tr.append("<td>" + sum.rprofit/100 + "</td>");
        tr.append("<td>" + sum.rback/100 + "</td>");
        table.append(tr);

        // 明细
        for (var i in list) {
            var o = list[i];
            var tr = $("<tr></tr>");
            //tr.append("<td>" + o.id + "</td>");
            tr.append("<td>" + Util.converServerTime(o.time) + "</td>");
            tr.append("<td>" + o.user.mobile + "</td>");
            tr.append("<td>" + get_game_display_name(o.type) + "</td>");
            tr.append("<td>" + o.issue + "</td>");
            tr.append("<td>" + Util.convertTarget(o.type, o.target) + "@" + o.rate + "</td>");
            tr.append("<td>" + o.amount/100 + "</td>");
            tr.append("<td>" + (o.profit/100).toFixed(3) + "</td>");
            tr.append("<td>" + (o.back || 0)/100 + "</td>");
            tr.append("<td>" + (o.rprofit || 0)/100 + "</td>");
            tr.append("<td>" + (o.rback || 0)/100 + "</td>");
            table.append(tr);
        }
    });
}

function balanceLogQuery() {
    Util.queryTemplate($(".zy-table tbody"), ".zxf_pagediv", $(".f10 button"), 
    function(page, size, cb) {
        var data = {
            pageSize: size,
            pageNum: page,
        };
        var v = $(".f1 input").val();
        if (v) {
            data.start = v;
        }
        v = $(".f2 input").val();
        if (v) {
            data.end = v;
        }
        v = $(".f3 input").val().trim();
        if (v) {
            data.uid = v;
        }
        v = $(".f4 select").val();
        if (v != -1) {
            data.type = v;
        }
        v = $(".f5 select").val();
        if (v != -1) {
            data.state = v;
        } 
        CMD.account_log_rquery(data, cb);
    }, function(table, list, sum) {
        if (Util.isEmpty(list)) {
            table.append('<tr><td colspan="8">暂无数据</td></tr>');
            return;
        }

        // 合计
        var tr = $("<tr></tr>");
        tr.append("<td colspan='4'>合计</td>");
        tr.append("<td>" + ((sum || 0)/100).toFixed(2) + "</td>");
        tr.append("<td colspan='3'></td>");
        table.append(tr);

        // 明细
        for (var i in list) {
            var d = list[i];
            var tr = $("<tr></tr>");
            tr.append("<td>" + d.start.substring(5) + "</td>");
            if (!d.end)
                tr.append("<td>--</td>");
            else
                tr.append("<td>" + d.end.substring(5) + "</td>");
            tr.append("<td class='name'>" + (d.user.mobile || '游客') + "</td>");
            
            tr.append("<td>" + Util.convertLogType(d.type) + (d.channel2 ? "/" + Util.convertChannel(d.channel2) : '') + "</td>");
            tr.append("<td>" + (d.amount/100) + "</td>");
            tr.append("<td>" + (d.balance/100) + "</td>");
            var memo = Util.convertMemo(d);
            tr.append("<td class='l'>" + memo + "</td>");
            tr.append("<td>" + Util.drawBalanceLogState(d) + "</td>");
            table.append(tr);
        }
    });
}

// 提款记录
function withdrawLog() {
    $(".f1 select").selected();
    $(".f2 input").datepicker({format: 'yyyy-mm-dd'});
    $(".f3 input").datepicker({format: 'yyyy-mm-dd'});

    Util.queryTemplate($(".zy-table tbody"), ".zxf_pagediv", $(".f10 button"), function(page, size, cb) {
        var data = {
            owner: "",
            type: "withdraw", // TODO
            pageSize: size,
            pageNum: page,
        };
        var v = $(".f1 select").val();
        if (v != -1) {
            data.state = v;
        }
        v = $(".f2 input").val();
        if (v) data.start = v;
        v = $(".f3 input").val();
        if (v) data.end = v;
        CMD.query_account_log2(data, cb);
    }, function(table, list, sum) {
        if (Util.isEmpty(list)) {
            table.append('<tr><td colspan="6">暂无数据</td></tr>');
            return;
        }

        // 合计
        var tr = $("<tr></tr>");
        tr.append("<td colspan='3'>合计</td>");
        tr.append("<td>" + sum/100 + "</td>");
        tr.append("<td colspan='2'></td>");
        table.append(tr);

        // 明细
        for (var i in list) {
            var d = list[i];
            var tr = $("<tr></tr>");
            tr.append("<td>" + d.id + "</td>");
            tr.append('<td>' + d.start + '</td>');
            tr.append('<td>' + (d.end || '') + '</td>');
            tr.append("<td>" + d.amount/100 + "</td>");
            var s = "申请提款__v__元";
            s = s.replace(/__c__/, Util.convertChannel(d.channel));
            s = s.replace(/__v__/, d.amount/100);
            tr.append('<td>' + s + '</td>');
            if (d.state == 0) tr.append('<td>待处理</td>');
            else if (d.state == 1) tr.append('<td>成功</td>');
            else if (d.state == 2) tr.append('<td>失败</td>');
            table.append(tr);
        }
    });
}

// 会员报表
function memberStat() {
    $(".f3 input").datepicker({format: 'yyyy-mm-dd'});
    $(".f4 input").datepicker({format: 'yyyy-mm-dd'});
    Util.queryTemplate($(".zy-table tbody"), ".zxf_pagediv", $(".f10 button"), function(page, size, cb) {
        var data = {
            pageSize: size,
            pageNum: page || 1,
        };
        var v = $(".f1 select").val();
        if (v != -1) {
            data.type = v;
        }
        v = $(".f2 input").val();
        if (v) data.uid = v;
        v = $(".f3 input").val();
        if (v) data.start = v;
        v = $(".f4 input").val();
        if (v) data.end = v;
        v = $(".f5 input").val();
        if (v) data.profitStart = v;
        v = $(".f6 input").val();
        if (v) data.profitEnd = v;
        CMD.stat_user_referee(data, cb);
    }, function(table, list, sum) {
        // 合计
        if (Util.isEmpty(list)) {
            table.append("<tr><td colspan='11'>暂无数据</td></tr>");
            return;
        }

        var tr = $("<tr></tr>");
        tr.append("<td>合计</td>");
        tr.append("<td>" + sum.betCount + "</td>");
        tr.append("<td>" + sum.betSum/100 + "</td>");
        tr.append("<td>" + sum.betWin/100 + "</td>");
        tr.append("<td>" + sum.rewardSum/100 + "</td>");
        tr.append("<td>" + "0/0" + "</td>");
        tr.append("<td>" + sum.betRprofit/100 + "</td>");
        tr.append("<td>" + sum.betRback/100 + "</td>");
        tr.append("<td>" + sum.betProfit/100 + "</td>");
        tr.append("<td>" + sum.betBack/100 + "</td>");
        tr.append("<td>" + (sum.betBack/100 + sum.betProfit/100) + "</td>");
        table.append(tr);

        // 明细
        for (var i in list) {
            var d = list[i];
            var tr = $("<tr></tr>");
            tr.append("<td>" + d.user.mobile + "</td>");
            tr.append('<td>' + d.betCount + '</td>');
            tr.append('<td>' + d.betSum/100 + '</td>');
            tr.append('<td>' + d.betWin/100 + '</td>');
            tr.append('<td>' + d.rewardSum/100 + '</td>');
            tr.append('<td>' + "0/0" + '</td>');
            tr.append('<td>' + d.betRprofit/100 + '</td>');
            tr.append('<td>' + d.betRback/100 + '</td>');
            tr.append('<td>' + d.betProfit/100 + '</td>');
            tr.append('<td>' + d.betBack/100 + '</td>');
            tr.append('<td>' + (d.betProfit/100 + d.betBack/100) + '</td>');
            table.append(tr);
        }
    });
}

// 会员管理
function memberManager() {
    Util.queryTemplate($(".zy-table tbody"), ".zxf_pagediv", $(".f10 button"), 
    function(page, size, cb) {
        var data = {
            pageSize: size,
            pageNum: page,
        };
        var v = $(".f1 select").val();
        if (v != -1) {
            data.state = v;
        }
        v = $(".f3 input").val();
        if (v) {
            data[$(".f2 select").val()] = v;
        }
        CMD.user_rquery(data, cb);
    }, function(table, list, sum) {
        if (Util.isEmpty(list)) {
            table.append('<tr><td colspan="10">暂无数据</td></tr>');
            return;
        }

        // 明细
        for (var i in list) {
            var d = list[i];
            var tr = $("<tr></tr>");
            if (d.online == 1)
                tr.append("<td style='color:blue;'>" + d.mobile + "</td>");
            else
                tr.append("<td style='color:gray;'>" + d.mobile + "</td>");
            tr.append("<td>" + (d.balance/100).toFixed(2) + "</td>");
            if (d.state == 0)
                tr.append("<td>正常</td>");
            else if (d.state == 1) {
                tr.append("<td>冻结</td>");
            }
            else {
                tr.append("<td>停用</td>");
            }
            tr.append("<td>" + (d.qq || '') + "</td>");
            tr.append("<td>" + new Date(d.register).Format("MM/dd HH:mm") + "</td>");
            tr.append("<td>" + new Date(d.login).Format("MM/dd HH:mm") + "</td>");
            tr.append("<td>" + d.depositCount + "</td>");
            tr.append("<td>" + d.deposit/100 + "</td>");
            tr.append("<td>" + d.withdrawCount + "</td>");
            tr.append("<td>" + d.withdraw/100 + "</td>");
            table.append(tr);
        }
    });
}

// 结算明细
function settleDetail() {
    var f = function() {
        var data = {
            pageSize: 400,
            pageNum: 1,
        }
        var v = $(".f1 select").val();
        if (v != -1) data.state = v;
        var v = $(".f2 input").val();
        if (v) data.start = v;
        var v = $(".f3 input").val();
        if (v) data.end = v;

        startLoading();
        CMD.stat_referee_query_user(data, function(r) {
            stopLoading();
            // 填充列表
            var table = $(".zy-table tbody"); table.empty();
            if (Util.isEmpty(r.list)) {
                table.append("<tr><td colspan='11'>暂无数据</td></tr>");
                return;
            }

            for (var i in r.list) {
                var o = r.list[i];
                var tr = $("<tr></tr>");
                tr.append("<td>" + o.month + "</td>");
                tr.append("<td>" + o.valid + "</td>");
                tr.append("<td>" + o.standard + "</td>");
                if (o.type == 1) {
                    tr.append("<td>--</td>");
                    tr.append("<td>--</td>");
                    tr.append("<td>" + o.profit/100 + "</td>");
                    tr.append("<td>" + o.back/100 + "</td>");
                }
                else {
                    // 模式2的数据
                    tr.append("<td></td>");
                    tr.append("<td></td>");
                    tr.append("<td></td>");
                    tr.append("<td></td>");
                }

                // 平台费用
                var s = o.riskFee * o.riskRate + o.adminFee * o.adminRate;
                s /= 10000;
                tr.append("<td>" + s + "</td>");

                // 累积佣金
                tr.append("<td>" + o.accumulate/100 + "</td>");

                // 实际佣金
                tr.append("<td>" + o.commission/100 + "</td>");

                // 状态
                var s = "";
                if (o.state == 0) {
                    s = "未结算";
                }
                else if (o.state == 1) {
                    s = "已累积";
                }
                else if (o.state == 2) {
                    s = "已结算";
                }
                else if (o.state == 3) {
                    s = "已清除";
                }
                if (o.check == 1) {
                    s += "/审核中";
                }
                else if (o.check == 2) {
                    s += "/审核通过";
                }
                tr.append("<td>" + s + "</td>");
                table.append(tr);
            }
        });
    };
    f();
    $(".f10 button").click(f);
}

function fillGames(s) {
    var arr = get_game_list();
    for (var i in arr) {
        s.append('<option value="' + arr[i] + '">' + get_game_display_name(arr[i]) + '</option>');
    }
}
